#include "hal_function.h"
#include "oled.h"
#include "cynfsm.h"
#include "homepage.h"
#include "oled_mgr.h"

static HometHandle *homeHanlde;

uint32_t showHomePage_Entry(uint32_t arg)
{
    homeHanlde = malloc(sizeof(HometHandle));
    memset(homeHanlde,0,sizeof(HometHandle));


    homeHanlde->resources.screen = lv_obj_create(NULL);

    lv_obj_remove_style_all(homeHanlde->resources.screen);
    lv_style_init(&homeHanlde->styles.screenstyle);
    lv_style_reset(&homeHanlde->styles.screenstyle);
    
    lv_style_set_x(&homeHanlde->styles.screenstyle, 0);
    lv_style_set_y(&homeHanlde->styles.screenstyle, 0);

    lv_style_set_width(&homeHanlde->styles.screenstyle,SSD1306_WEIGHT);
    lv_style_set_height(&homeHanlde->styles.screenstyle,SSD1306_HEIGHT);
    lv_style_set_bg_opa(&homeHanlde->styles.screenstyle, LV_OPA_COVER); 
    lv_style_set_bg_color(&homeHanlde->styles.screenstyle, lv_color_black());

    lv_style_set_pad_bottom(&homeHanlde->styles.screenstyle, 0);
    lv_style_set_pad_left(&homeHanlde->styles.screenstyle, 0);
    lv_style_set_pad_right(&homeHanlde->styles.screenstyle, 0);
    lv_style_set_pad_row(&homeHanlde->styles.screenstyle, 0);
    lv_style_set_pad_column(&homeHanlde->styles.screenstyle, 0);

    lv_style_set_outline_pad(&homeHanlde->styles.screenstyle, 0);
    lv_style_set_border_width(&homeHanlde->styles.screenstyle, 0);

    lv_style_set_text_color(&homeHanlde->styles.screenstyle, lv_color_white());
    lv_obj_add_style(homeHanlde->resources.screen,&homeHanlde->styles.screenstyle,0); 
    lv_scr_load(homeHanlde->resources.screen);


    homeHanlde->resources.time_label = lv_label_create(homeHanlde->resources.screen);
    lv_style_set_text_color(&homeHanlde->styles.time_label, lv_color_white());
    lv_obj_add_style(homeHanlde->resources.time_label,&homeHanlde->styles.time_label,0);
    lv_obj_set_width(homeHanlde->resources.time_label, 80);
    lv_obj_set_align(homeHanlde->resources.time_label,LV_ALIGN_TOP_RIGHT);

    homeHanlde->resources.wifi = lv_label_create(homeHanlde->resources.screen);
    
    lv_style_set_text_color(&homeHanlde->styles.wifi_label, lv_color_white());
    lv_obj_add_style(homeHanlde->resources.time_label,&homeHanlde->styles.wifi_label,0);
    lv_obj_set_width(homeHanlde->resources.wifi, 50);
    lv_obj_set_pos(homeHanlde->resources.wifi,80,48);
    lv_obj_set_scroll_dir(homeHanlde->resources.wifi,LV_DIR_HOR);
    //lv_obj_set_align(homeHanlde->resources.wifi,LV_ALIGN_BOTTOM_MID);
    lv_label_set_text(homeHanlde->resources.wifi, LV_SYMBOL_OK LV_SYMBOL_WIFI LV_SYMBOL_PLAY);

    lv_obj_add_flag(homeHanlde->resources.wifi,LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_scroll_to_x(homeHanlde->resources.wifi,0,LV_ANIM_ON);

    lv_event_send(homeHanlde->resources.wifi, LV_EVENT_SCROLL, NULL);
    return 0;
}

uint32_t showHomePage_Exit(uint32_t arg)
{
    if(homeHanlde)
    {
        lv_obj_del(homeHanlde->resources.screen);
        free(homeHanlde);
        homeHanlde = 0;
    }
    return 0;
}




static void delButtonMSG(uint8_t button)
{
    ESP_LOGI("RM", "Button->[%d]",button); 
    switch(button)
    {
        case UI_BUTTON_NUM_UP:
           break;
        default:
            break;
    }
}

static void msgDeal(oledMessage *msg)
{
    if( msg )
    {
        switch(msg->source_page)
        {
            case OLED_PAGE_MAX:
            {
                switch(msg->data.max.datatype)
                {
                    case OLED_EX_DATATYPE_BUTTON:
                    {
                        delButtonMSG(msg->data.max.data.button.button);
                        break;
                    }
                }

                break;
            }    
            default:
            break;
        }
    }
}



uint32_t showHomePage_Process(uint32_t arg)
{
    char time_arr[50];
    time_t now =time(NULL);
    struct tm tmr;
    now += (8*60*60);
    localtime_r(&now, &tmr);
    tmr.tm_year += 1900;
    tmr.tm_mon += 1;
    if(tmr.tm_year <2000 ) tmr.tm_year = 2000;
    snprintf(time_arr,sizeof(time_arr),"%02d-%02d-%02d %02d:%02d:%02d\0",tmr.tm_year%2000,tmr.tm_mon,tmr.tm_mday,tmr.tm_hour,tmr.tm_min,tmr.tm_sec);
    lv_label_set_text(homeHanlde->resources.time_label,time_arr);
   return 0;
}


